package com.graplo.gwtgaefb.client;

import com.google.gwt.core.client.JavaScriptObject;

/**
 *
 * @author Dawid Ostrowski
 *
 * This class contains calls to JavaScript methods of JS Facebook API wrapped in static native Java methods
 * Use this class if you want full control of parameters etc.
 * Many people will find easier to use HandyFacebookClient class
 *
 * @see HandyFacebookClient
 *
 */
public class FacebookClient {

	/**
	 * FB.api makes API calls to the Graph API or Deprecated REST API.
	 *
	 * @param path - graph api path. like /platform
	 * @param responseHandler - handle method of this object will be invoked when response arrives
	 * @see <a href="http://developers.facebook.com/docs/reference/javascript/FB.api/">http://developers.facebook.com/docs/reference/javascript/FB.api/</a>
	 */
	public static native void api(String path, ResponseHandler responseHandler) /*-{
		$wnd.FB.api(path, function(response) {
		  responseHandler.@com.graplo.gwtgaefb.client.ResponseHandler::handle(Lcom/google/gwt/core/client/JavaScriptObject;)(response);
		});
	}-*/;

	/**
	 * FB.api makes API calls to the Graph API or Deprecated REST API.
	 *
	 * @param path - graph api path. like /me
	 * @param params - A JavaScriptObject like { limit: 3 }
	 * @param responseHandler - handle method of this object will be invoked when reponse arrives
	 * @see <a href="http://developers.facebook.com/docs/reference/javascript/FB.api/">http://developers.facebook.com/docs/reference/javascript/FB.api/</a>
	 */
	public static native void api(String path, JavaScriptObject params, ResponseHandler responseHandler) /*-{
		$wnd.FB.api(path, params, function(response) {
		  responseHandler.@com.graplo.gwtgaefb.client.ResponseHandler::handle(Lcom/google/gwt/core/client/JavaScriptObject;)(response);
		});
	}-*/;

	/**
	 * FB.api makes API calls to the Graph API or Deprecated REST API.
	 *
	 * @param path - graph api path. like /me/feed
	 * @param method - http method like "post"
	 * @param params - A JavaScriptObject like { message: body }
	 * @param responseHandler - handle method of this object will be invoked when response arrives
	 * @see <a href="http://developers.facebook.com/docs/reference/javascript/FB.api/">http://developers.facebook.com/docs/reference/javascript/FB.api/</a>
	 */
	public static native void api(String path, String method, JavaScriptObject params, ResponseHandler responseHandler) /*-{
		$wnd.FB.api(path, method, params, function(response) {
		  responseHandler.@com.graplo.gwtgaefb.client.ResponseHandler::handle(Lcom/google/gwt/core/client/JavaScriptObject;)(response);
		});
	}-*/;

	/**
	 *
	 * @param params - A JavaScriptObject that contains <b>method</b> and other parameters like:
	 * <pre>
	 * {method: 'feed',
     *	name: 'Facebook Dialogs',
     *	link: 'http://developers.facebook.com/docs/reference/dialogs/',
     *	picture: 'http://fbrell.com/f8.jpg',
     *	caption: 'Reference Documentation',
     *	description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'}
	 * </pre>
	 * @param responseHandler - handle method of this object will be invoked when response arrives. Not all ui dialogs will provide response
	 * @see <a href="http://developers.facebook.com/docs/reference/javascript/FB.ui/">http://developers.facebook.com/docs/reference/javascript/FB.ui/</a>
	 */
	public static native void ui(JavaScriptObject params, ResponseHandler responseHandler) /*-{
		$wnd.FB.ui(params, function(response) {
		  responseHandler.@com.graplo.gwtgaefb.client.ResponseHandler::handle(Lcom/google/gwt/core/client/JavaScriptObject;)(response);
		});
	}-*/;


	/**
	 * Attaches an handler to an event and invokes your responseHandler when the event fires.
	 * @param eventName - e.g. 'auth.login'
	 * @param responseHandler - handle method of this object will be invoked when response arrives
	 * @see <a href="http://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/">http://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/</a>
	 */
	public static native void eventSubscribe(String eventName, ResponseHandler responseHandler) /*-{
		$wnd.FB.Event.subscribe(eventName, function(response) {
  			responseHandler.@com.graplo.gwtgaefb.client.ResponseHandler::handle(Lcom/google/gwt/core/client/JavaScriptObject;)(response);
		});
	}-*/;

	/**
	 * Removes handlers on events so that it no longer invokes your callback when the event fires.
	 * @param eventName - e.g. 'auth.login'
	 * @param responseHandler - handle method of this object will be invoked when response arrives
	 * @see <a href="http://developers.facebook.com/docs/reference/javascript/FB.Event.unsubscribe/">http://developers.facebook.com/docs/reference/javascript/FB.Event.unsubscribe/</a>
	 */
	public static native void eventUnsubscribe(String eventName, ResponseHandler responseHandler) /*-{
		$wnd.FB.Event.unsubscribe(eventName, function(response) {
  			responseHandler.@com.graplo.gwtgaefb.client.ResponseHandler::handle(Lcom/google/gwt/core/client/JavaScriptObject;)(response);
		});
	}-*/;

	/**
	 * <b>Synchronous</b> accessor for the current authResponse.
	 * @see <a href="http://developers.facebook.com/docs/reference/javascript/FB.getAuthResponse/">http://developers.facebook.com/docs/reference/javascript/FB.getAuthResponse/</a>
	 * @return Returns just the authResponse object
	 */
	public static native JavaScriptObject getAuthResponse() /*-{
		return $wnd.FB.getAuthResponse();
	}-*/;

	/**
	 * FB.getLoginStatus allows you to determine if a user is logged in and connected to your app.
	 *
	 * @param responseHandler - handle method of this object will be invoked when response arrives
	 * @see <a href="http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/">http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/</a>
	 */
	public static native void getLoginStatus(ResponseHandler responseHandler) /*-{
		$wnd.FB.getLoginStatus(function(response) {
				responseHandler.@com.graplo.gwtgaefb.client.ResponseHandler::handle(Lcom/google/gwt/core/client/JavaScriptObject;)(response);
		});
	}-*/;

	/**
	 * FB.login prompts the user to authorize your application.
	 * You should only call FB.login when bound to a user driven click.
	 * Most browsers block popups, unless they were initiated from a user event, such as a click on a button or a link.
	 *
	 * @param responseHandler - handle method of this object will be invoked when response arrives
	 *
	 * @see <a href="http://developers.facebook.com/docs/reference/javascript/FB.login/">http://developers.facebook.com/docs/reference/javascript/FB.login/</a>
	 */
	public static native void login(ResponseHandler responseHandler) /*-{
		$wnd.FB.login(function(response) {
				responseHandler.@com.graplo.gwtgaefb.client.ResponseHandler::handle(Lcom/google/gwt/core/client/JavaScriptObject;)(response);
		});
	}-*/;

	/**
	 * FB.login prompts the user to authorize your application.
	 * You should only call FB.login when bound to a user driven click.
	 * Most browsers block popups, unless they were initiated from a user event, such as a click on a button or a link.
	 *
	 * @param responseHandler - handle method of this object will be invoked when response arrives
	 * @param params - scope params like: {scope: 'email'}
	 *
	 * @see <a href="http://developers.facebook.com/docs/reference/javascript/FB.login/">http://developers.facebook.com/docs/reference/javascript/FB.login/</a>
	 */
	public static native void login(ResponseHandler responseHandler, JavaScriptObject params) /*-{
		$wnd.FB.login(function(response) {
				responseHandler.@com.graplo.gwtgaefb.client.ResponseHandler::handle(Lcom/google/gwt/core/client/JavaScriptObject;)(response);
		}, params);
	}-*/;

	/**
	 * Log the user out of your site and Facebook
	 *
	 * @param responseHandler - handle method of this object will be invoked when response arrives
	 *
	 * @see <a href="http://developers.facebook.com/docs/reference/javascript/FB.logout/">http://developers.facebook.com/docs/reference/javascript/FB.logout/</a>
	 */
	public static native void logout(ResponseHandler responseHandler) /*-{
		$wnd.FB.logout(function(response) {
				responseHandler.@com.graplo.gwtgaefb.client.ResponseHandler::handle(Lcom/google/gwt/core/client/JavaScriptObject;)(response);
		});
	}-*/;
}
